<?php

namespace app\wmj\controller;

use support\Request;
use app\controller\WmjBaseController;
class W77Controller
{

    protected $noNeedLogin = ['index'];
    public function index(Request $request)
    {
        return response(__CLASS__);
    }
    //远程开门
    public function openDoor(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $uid = $request->post('uid','');
        if(!$uid){
            return json(['code'=>0,'msg'=>'缺失参数uid']);
        }
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "type" => 1,
            "data" => [
                "cmd_type" => "open",
                "info"=>[
                    "uid"=>$uid
                ]
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
    //添加人脸
    public function faceAdd(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $name = $request->post('name','');
        if(!$name){
            return json(['code'=>0,'msg'=>'缺失参数name']);
        }
        $face_id = $request->post('face_id','');
        if(!$face_id){
            return json(['code'=>0,'msg'=>'缺失参数face_id']);
        }
        $img_url = $request->post('img_url','');
        if(!$img_url){
            return json(['code'=>0,'msg'=>'缺失参数img_url']);
        }
        $info['name'] = $name;
        $info['face_id'] = $face_id;
        $info['img_url'] = $img_url;
        $phone_number = $request->post('phone_number','');//电话号码
        $start_time = $request->post('start_time',0);//有效开始时间戳
        $end_time = $request->post('end_time',0);//有效结束时间戳
        $type = $request->post('type',0);// 0永久有效 1时间段有效 16人脸识别通知到服务端
        if($phone_number){
            $info['phone_number'] = $phone_number;
        }
        if($start_time){
            $info['start_time'] = $start_time;
        }
        if($end_time){
            $info['end_time'] = $end_time;
        }
        $info['type'] = $type;
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "type" => 1,
            "data" => [
                "cmd_type" => "face_add",
                "info"=>$info
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
    //人脸删除
    public function faceDel(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $face_id = $request->post('face_id','');
        if(!$face_id){
            return json(['code'=>0,'msg'=>'缺失参数face_id']);
        }
        $info['face_id'] = $face_id;
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "type" => 1,
            "data" => [
                "cmd_type" => "face_del",
                "info"=>$info
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
    //人脸查找
    public function faceFind(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $face_id = $request->post('face_id','');
        if(!$face_id){
            return json(['code'=>0,'msg'=>'缺失参数face_id']);
        }
        $info['face_id'] = $face_id;
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "type" => 1,
            "data" => [
                "cmd_type" => "face_find",
                "info"=>$info
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
    //人脸信息更新
    public function faceEdit(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $name = $request->post('name','');
        if(!$name){
            return json(['code'=>0,'msg'=>'缺失参数name']);
        }
        $face_id = $request->post('face_id','');
        if(!$face_id){
            return json(['code'=>0,'msg'=>'缺失参数face_id']);
        }
        $img_url = $request->post('img_url','');
        if(!$img_url){
            return json(['code'=>0,'msg'=>'缺失参数img_url']);
        }
        $info['name'] = $name;
        $info['face_id'] = $face_id;
        $info['img_url'] = $img_url;
        $phone_number = $request->post('phone_number','');//电话号码
        $start_time = $request->post('start_time',0);//有效开始时间戳
        $end_time = $request->post('end_time',0);//有效结束时间戳
        $type = $request->post('type',0);// 0永久有效 1时间段有效 16人脸识别通知到服务端
        if($phone_number){
            $info['phone_number'] = $phone_number;
        }
        if($start_time){
            $info['start_time'] = $start_time;
        }
        if($end_time){
            $info['end_time'] = $end_time;
        }
        $info['type'] = $type;
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "type" => 1,
            "data" => [
                "cmd_type" => "face_edit",
                "info"=>$info
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
    //删除所有人脸
    public function faceClr(Request $request){
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $info = [];
        $WmjBase = new WmjBaseController();
        $data = [
            "device_sn" => $device_sn,
            "type" => 1,
            "data" => [
                "cmd_type" => "face_clr",
                "info"=>$info
            ],
        ];
        $res = $WmjBase->Request('send', $data,$app_id,$app_secret);
        return json($res);
    }
}
